package panel.selfPathList
{
	import assists.Lang;
	import assists.URI;
	import assists.log.Log;
	import assists.resource.ResourceLoader;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	
	import struct.SelfPath;
	
	public class SelfPathEditContainer extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var allBoxList:Array;
		
		private var refreshList:Function;
		
		public var pathData:SelfPath;
		
		public function SelfPathEditContainer(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			allBoxList = [];
		}
		
		public function changePathInfo():void
		{
			var list:Array = changeToList();
			
			//			for(var i:int = 0;i < list.length;i++){
			//				var str:String = "";
			//				
			//				for(var j:int = 0;j < list[i].length;j++){
			//					if(j < list[i].length - 1){
			//						str = str + list[i][j].id + "@" + list[i][j].type + "^";
			//					}else{
			//						str = str + list[i][j].id + "@" + list[i][j].type;
			//					}
			//				}
			//				pathData.info[i] = str;
			//			}
			
			pathData.info = list;
		}
		
		private function changeToList():Array
		{
			var date:int = 0;
			var list:Array = [];
			
			for(var i:int = 0;i < allBoxList.length;i++){
				if(allBoxList[i].currentDate != date){	
					list[date] = [];
					
					list[date].push(allBoxList[i].info);
					date ++;
				}else{
					list[date - 1].push(allBoxList[i].info);
				}
			}
			
			return list;
		}
		
		public function changeData(list:Array):void
		{
			var box:SelfPathEditBox;
			var count:int = 0;
			for(var i:int = 0;i < list.length;i++){
				for(var j:int = 0;j < list[i].length;j++){
					box = new SelfPathEditBox(_stage,_loader,_lang,_uri);
					box.onUp = upOne;
					box.onDown = downOne;
					box.onDelete = deleteOne;
					box.onReList = reList;
					box.date = i;
					
					box.changeData(list[i][j]);
					this.addChild(box);
					
					box.x = 0;
					box.y = box.height * count;
					
					allBoxList.push(box);
					
					count ++;
				}
			}
		}
		
		private function upOne(info:*):void
		{
			for(var i:int = 0;i < allBoxList.length;i++){
				if(allBoxList[i].info == info){
					if(i > 0 && allBoxList[i].currentDate == allBoxList[i - 1].currentDate){
						allBoxList[i].y = allBoxList[i].y - allBoxList[i].height;
						allBoxList[i - 1].y = allBoxList[i - 1].y + allBoxList[i -1].height;
						
						var nima:* = allBoxList[i];
						allBoxList[i] = allBoxList[i - 1];
						allBoxList[i - 1] = nima;
						
						break;
					}else{
						return;
					}
				}
			}
			
			if(refreshList is Function){
				refreshList();
			}
			
			//			changePathInfo();
		}
		
		private function downOne(info:*):void
		{
			for(var i:int = 0;i < allBoxList.length;i++){
				if(allBoxList[i].info == info){
					if(i < (allBoxList.length - 1)  && allBoxList[i].currentDate == allBoxList[i + 1].currentDate){
						allBoxList[i].y = allBoxList[i].y + allBoxList[i].height;
						allBoxList[i + 1].y = allBoxList[i + 1].y - allBoxList[i + 1].height;
						
						var nima:* = allBoxList[i];
						allBoxList[i] = allBoxList[i + 1];
						allBoxList[i + 1] = nima;
						
						break;
					}else{
						return;
					}
				}
			}
			
			if(refreshList is Function){
				refreshList();
			}
			
			//			changePathInfo();
		}
		
		private function deleteOne(info:*):void
		{
			for(var i:int = 0;i < allBoxList.length;i++){
				if(allBoxList[i].info == info){
					this.removeChild(allBoxList[i]);
					allBoxList.splice(i,1);
					
					break;
				}
			}
			
			for(var j:int = 0;j < allBoxList.length;j++){
				allBoxList[j].y = allBoxList[j].height * j;
			}
			
			if(refreshList is Function){
				refreshList();
			}
			
			//			changePathInfo();
		}
		
		private function reList():void
		{
			allBoxList.sortOn("currentDate",Array.NUMERIC);
			
			for(var j:int = 0;j < allBoxList.length;j++){
				allBoxList[j].y = allBoxList[j].height * j;
			}
			
			if(refreshList is Function){
				refreshList();
			}
			
			//			changePathInfo();
		}
		
		public function set refresh(func:Function):void
		{
			refreshList = func;
		}
		
		public function clear():void
		{
			allBoxList = [];
		}
	}
}